package com.gexiaobao.pigeon.ui.fragment.mine.organization.detail

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentHistoryDrippingPigeonBinding
import com.gexiaobao.pigeon.ui.adapter.AdapterDrippingOrder
import com.gexiaobao.pigeon.ui.dialog.RxDialogDefault
import com.gexiaobao.pigeon.ui.fragment.mine.organization.activity.ActivityDrippingOrderDetail
import com.gexiaobao.pigeon.viewmodel.DrippingViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.util.startActivity

/**
 * @Author      : hxw
 * @Date        : 2024/7/8 15:33
 * @Describe    :
 */
class FragmentHistoryDrippingPigeon : BaseFragment<DrippingViewModel, FragmentHistoryDrippingPigeonBinding>() {

    private val mAdapter: AdapterDrippingOrder by lazy { AdapterDrippingOrder(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var mState = 0

    override fun initView(savedInstanceState: Bundle?) {
        mState = requireArguments().getInt("state")

        //状态页配置
        loadsir = loadServiceInit(mDatabind.includeList.swipeRefreshLayout) {
            /** 点击重试时触发的操作 */
            onRefresh(true)
        }

        mDatabind.includeList.swipeRefreshLayout.init {
            onRefresh(true)
        }

        onRefresh(true)

        mDatabind.includeList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 10f)))
            it.initFooter {
                //触发加载更多时请求数据
                onRefresh(false)
            }
        }

        mAdapter.addChildClickViewIds(R.id.tvWithDrawAnOrder)
        mAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tvWithDrawAnOrder -> {
                    showDeleteDialog(mAdapter.data[position].id.toString())
                }
            }
        }

        mAdapter.setOnItemClickListener { _, _, position ->
            startActivity<ActivityDrippingOrderDetail>("id" to mAdapter.data[position].id.toString())
        }
    }

    override fun createObserver() {
        super.createObserver()
        /**  删除/撤销订单 */
        mViewModel.deleteDrippingOrderResult.observe(this) {
            onRefresh(true)
        }
        /** 获取订单列表（用户角色）  */
        mViewModel.drippingOrderResult.observe(this) {
            loadListData(it, mAdapter, loadsir, mDatabind.includeList.swipeRecyclerView, mDatabind.includeList.swipeRefreshLayout)
        }
    }

    private fun showDeleteDialog(id: String) {
        val dialog = RxDialogDefault(context)
        dialog.setContent("当天内最多可操作3次撤单，超出则不可再次进行发单，请谨慎操作！")
        dialog.setLeftBtnContent("再想想")
        dialog.setRightBtnContent("确认撤单")
        dialog.setSureListener {
            mViewModel.deleteOrderDidi(id)
            dialog.dismiss()
        }
        dialog.setFullScreenWidth()
        dialog.show()
    }

    private fun onRefresh(b: Boolean) {
        loadsir.showLoading()
        mViewModel.getOrderDidiListByUserId(b, mState)
    }

    companion object {
        fun newInstance(state: Int): FragmentHistoryDrippingPigeon {
            val fragment = FragmentHistoryDrippingPigeon()
            val bundle = Bundle()
            bundle.putInt("state", state)
            fragment.arguments = bundle
            return fragment
        }
    }

}